#!/bin/env ruby
# encoding: utf-8

require_relative "date"


def check_all_links
  selectList = select_list(id: "js-report-selector-departments")

  selectList.options.each do |option|  
    selectList.focus()  
    selectList.select option.text()
    browser.ul(class: 'list-group').wait_until_present

    attribute=option.attribute_value('data-department-id') 
    if attribute!=nil then
      browser.ul(class: "list-group").links.each do |arr|
      arr.href.include?("departmentId=#{attribute.to_s}").should be_truthy
      end        
    end    
  end
end


def day(table)   
  res_day=""
  day_exists = false
  table.tbody.trs.each do |row|
    row.cells.each do |cell|
      if cell.class_name.include? 'available active' then
        res_day = cell.text()        
        day_exists = true                   
        break          
      end
    end            
  end
  day_exists.should be_truthy
  return  res_day
end

def month(table)
  col_count=table.cols.count
  res_month=""
  month_exists = false
  table.thead.trs.each do |row|
    row.cells.each do |cell|
      if cell.class_name == 'month' then
        res_month = cell.text()
        month_exists = true                   
        break
      end
    end            
  end
  month_exists.should be_truthy
  return  res_month
end


def check_interval(num,a,b)
  browser.element(:xpath, "html/body/div[#{num}]").wait_until_present 
  tables[a].wait_until_present
  tables[b].wait_until_present
  start_day=day(tables[a])
  end_day=day(tables[b])
  start_month=month(tables[a])
  end_month=month(tables[b])
 
  start_date=Hello::Date.new(start_day, start_month)
  end_date  =Hello::Date.new(end_day, end_month)  
#нужен click?
  browser.element(:xpath, "html/body/div[#{num}]/div[3]/div/button[1]").when_present.click()
  return(start_date<=end_date) 
end

def test_for_links()
  text_field(class: "form-control input-sm js-reports-rangepicker", index: 1).click() 

  hrefA="/en/us/standard-report-improvements-on-benefit-"
  selectList = select_list(id: "report-input-benefit")

  selectList.options.each do |option|  
    selectList.focus()  
    selectList.select option.text()
     
    attr_str=option.text().gsub!(" ", "%20")
    listElem = browser.element(:xpath, "html/body/div[2]/div/div[2]/div/div/div/ul/li[1]/ul/li[2]")
    
    aCSV = listElem.link(class: "report-link js-by-department", text: 'CSV').attribute_value('href')
    aPDF = listElem.link(class: "report-link js-by-department", text: 'PDF').attribute_value('href')
    array = aCSV.scan(/\?benefit=/) 
    if (array != nil) then
      substr = aCSV[aCSV.index(array[0])..aCSV.size].to_s
      (substr == "?benefit=#{attr_str.to_s}").should be_truthy
    end
    array = aPDF.scan(/\?benefit=/) 
    if (array != nil) then
      substr = aPDF[aPDF.index(array[0]),aPDF.size].to_s
      (substr == "?benefit=#{attr_str.to_s}").should be_truthy
    end   
  end
end


def check_req_links()
  selectList = select_list(id: "report-input-author")
  selectList.options.each do |opt|
    attribute=opt.attribute_value('data-user-id')
    hrefCSV="/en/us/standard-report-improvements-author-csv/#{attribute.to_s}"
    hrefPDF="/en/us/standard-report-improvements-author-pdf/#{attribute.to_s}" 
    STDERR.puts  "attribute = #{attribute.to_s}"
    STDERR.puts  "hrefCSV" + hrefCSV    
    STDERR.puts  "hrefPDF" + hrefPDF
    a(href: hrefPDF, text: 'PDF').wait_until_present  
    a(href: hrefCSV, text: 'CSV').wait_until_present    
  end   
end

def score_check_input
  check_interval(3,0,1)
end